package com.transcendence.appwidget.twofive;

import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.IBinder;
import android.widget.RemoteViews;

import androidx.annotation.Nullable;

import com.transcendence.appwidget.R;
import com.transcendence.core.utils.log.LogUtils;
import com.transcendence.core.utils.sp.SPUtils;

/**
 * @author joephone
 * @date 2023/9/14
 * @desc
 */
public class TwoFiveUpdateBgService extends Service {
//    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        updateRemoteViewImage();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        updateRemoteViewImage();
        return START_STICKY;

    }

    private void updateRemoteViewImage() {
        LogUtils.d("updateRemoteViewImage");
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.layout_two_five);
        int resourceId = R.drawable.bg_app_widget_gradient_default;
        int selected = SPUtils.getInstance().getInt("AppWidgetBgSelected",0);
        switch (selected){
            case 0:
                resourceId = R.drawable.bg_app_widget_gradient_default;
                break;
            case 1:
                resourceId = R.drawable.bg_app_widget_gradient_one;
                break;
            case 2:
                resourceId = R.drawable.bg_app_widget_gradient_two;
                break;
            case 3:
                resourceId = R.drawable.bg_app_widget_gradient_three;
                break;
            case 4:
                resourceId = R.drawable.bg_app_widget_gradient_four;
                break;
        }
        remoteViews.setImageViewResource(
                R.id.bg_appwidget,
                resourceId
        );

        ComponentName componentName =new ComponentName(getApplicationContext(), TwoFiveAppWidget.class);
        AppWidgetManager.getInstance(getApplicationContext())
                .updateAppWidget(componentName, remoteViews);
    }
}

